const crypto = require('crypto');
const base64url = require('base64url');

// 创建一个hmac对象
const hmac = crypto.createHmac('sha256', 'token');


const header = {"alg": "HS256", "typ": "JWT"};
const payload = {
    username: '小美',
    id: 1
// token：解析token的标识
}

const codeHeaderStr = JSON.stringify(header);
const codepayloadStr = JSON.stringify(payload);
const secret = base64url.encode(codeHeaderStr) + '.' + base64url.encode(codepayloadStr);
console.log(secret);


// 往hmac对象中添加摘要内容
const up = hmac.update(secret);

// 使用 digest 方法输出摘要内容
const result = up.digest('base64');

console.log(result);